package com.itheima.dao.mapper;

import com.itheima.pojo.Book;
import com.itheima.pojo.Borrow;
import com.itheima.pojo.History;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface BookMapper {
    /**
     * 查询所有图书
     * @return
     */
    @Select("select * from tb_book")
    List<Book> selectAll() ;

    /**
     * 增加图书
     * @param book
     */
    @Insert("insert into tb_book values(null,#{name},#{price},#{pnum},#{category})")
    boolean add(Book book);

    /**
     * 批量删除图书
     * @param ids
     */
    void deleteByIds(@Param("ids") int[] ids);

    /**
     *删除
     * @param id
     */
    @Delete("delete from tb_book where id = #{id}")
    void delete(int id);

    /**
     * 编辑图书
     * @param book
     */
    @Update("update tb_book set name = #{name},price = #{price},pnum = #{pnum},category = #{category} where id = #{id}")
    int update(Book book);

    /**
     * 分页查询
     * @param begin
     * @param size
     * @return
     */
    @Select("select * from tb_book limit #{begin} , #{size}")
    List<Book> selectByPage(@Param("begin") int begin,@Param("size") int size);


    /**
     * 查询总记录数
     * @return
     */
    @Select("select count(*) from tb_book")
    int selectTotalCount();


    /**
     * 分页条件查询
     * @param begin
     * @param size
     * @return
     */
    List<Book> selectByPageAndCondition(@Param("begin") int begin,@Param("size") int size,@Param("book") Book book);

    /**
     * 根据条件查询总记录数
     * @return
     */
    int selectTotalCountByCondition(Book book);

    /**
     * 通过id查找图书
     * @param id
     * @return
     */
    @Select("select * from tb_book where id = #{id}")
    Book selectById(int id);

    /**
     * 借书
     * @param borrow
     */
    @Insert("insert into tb_borrow values(null,#{time},#{username},#{name})")
    void borrow(Borrow borrow);

    /**
     * 还书时根据用户名查询借书的信息
     * @param username
     * @return
     */
    @Select("select * from tb_borrow where username = #{username}")
    List<Borrow> selectByUsername(String username);

    /**
     * 还书时把借书信息表的记录删除
     * @param id
     */
    @Delete("delete from tb_borrow where id = #{id}")
    void deleteBorrow(int id);

    /**
     * 还书时根据id获取对象
     * @param id
     * @return
     */
    @Select("select * from tb_borrow where id = #{id}")
    Borrow getBook(int id);

    /**
     * 根据名字从库存查询图书
     * @param name
     * @return
     */
    @Select("select * from tb_book where name = #{name}")
    Book selectByName(String name);

    /**
     * 根据名字归还图书到图书库存表
     * @param book
     */
    @Update("update tb_book set name = #{name},price = #{price},pnum = #{pnum},category = #{category} where name = #{name}")
    void huanShu(Book book);

    /**
     * 查询所有借书信息
     * @return
     */
    @Select("select * from tb_borrow")
    List<Borrow> selectFromBorrow();

    /**
     * 根据用户名删除借书记录
     * @return
     */
    @Delete("delete from tb_borrow where username = #{username}")
    int deleteUser(String username);

    /**
     * 借书历史记录
     * @param history
     * @return
     */
    @Insert("insert into tb_history values(null,#{username},#{name},#{borrowTime},#{time})")
    boolean addHistory(History history);

    /**
     * 根据用户名删除用户借书的历史记录
     * @param username
     * @return
     */
    @Delete("delete from tb_history where username = #{username}")
    int deleteHistory(String username);
}
